/**
 * 
 */
package com.yesep.learn.netty.tcp.client;

import java.net.SocketAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class ClientHandlerAdapter extends ChannelHandlerAdapter {
	private static final Logger log = LoggerFactory.getLogger(ClientHandlerAdapter.class);

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		log.error("通道" + ctx.channel().id() + "连接异常", cause);
		ctx.close();
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		Channel channel = ctx.channel();
		SocketAddress localAddr = channel.localAddress();
		SocketAddress remoteAddr = channel.remoteAddress();
		log.info("通道ID : {}  localAddress : {}  remoteAddress:{} 回应报文 : {}", channel.id(), localAddr, remoteAddr, msg);
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		super.channelReadComplete(ctx);
		ctx.flush();
	}
}